package club.kingyin.kyblogprouser.controller;



import club.kingyin.commons.constant.AuthorizeConstant;
import club.kingyin.commons.entity.user.Message;
import club.kingyin.commons.enums.BlogEnum;
import club.kingyin.commons.response.Result;
import club.kingyin.easycache.annotation.EasyCache;
import club.kingyin.kyblogprouser.core.cache.AttentionUserHandler;
import club.kingyin.kyblogprouser.service.MessageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import club.kingyin.commons.entity.user.Friend;
import club.kingyin.kyblogprouser.service.FriendService;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.net.URLDecoder;
import java.util.List;

/**
 * (Friend)表控制层
 *
 * @author makejava
 * @since 2022-01-20 15:03:37
 */
@RestController
@RequestMapping("friend")
public class FriendController {
    /**
     * 服务对象
     */
    @Resource
    private FriendService friendService;

//    @Resource
//    private MessageService messageService;
//
//    /**
//     * 分页查询所有数据
//     *
//     * @param page 分页对象
//     * @param friend 查询实体
//     * @return 所有数据
//     */
//    @GetMapping
//    public Result selectAll(Page<Friend> page, Friend friend) {
//        return Result.success(this.friendService.page(page, new QueryWrapper<Friend>(friend)));
//    }
//
//    /**
//     * 通过主键查询单条数据
//     *
//     * @param id 主键
//     * @return 单条数据
//     */
//    @GetMapping("{id}")
//    public Result selectOne(@PathVariable Serializable id) {
//        return Result.success(this.friendService.getById(id));
//    }
//
//    /**
//     * 新增数据
//     *
//     * @param friend 实体对象
//     * @return 新增结果
//     */
//    @PostMapping
//    public Result insert(@RequestBody Friend friend) {
//        return Result.success(this.friendService.save(friend));
//    }

    /**
     * 修改数据
     *
     * @param friend 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Result update(@RequestBody Friend friend) {
        return Result.success(this.friendService.updateById(friend));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result delete(@RequestParam("idList") List<Long> idList) {
        return Result.success(this.friendService.removeByIds(idList));
    }

    @EasyCache(handler = AttentionUserHandler.class)
    @EasyCache(prams = {"userid=id"}, delete = {"getUserDetail"}, module = "ky-user")
    @EasyCache(prams = {"userid=id"}, delete = {"getLikesInfo"}, module = "ky-user")
    @EasyCache(prams = {"userid=id"}, delete = {"getFansInfo"}, module = "ky-user")
    @EasyCache(prams = "userid=id", delete = "getUserInfo", module = "ky-user")
    @GetMapping("/change")
    @SneakyThrows
    public Result changeFriend(@RequestHeader(AuthorizeConstant.ID) int id,
                               @RequestHeader(AuthorizeConstant.AVATAR) String avatar,
                               @RequestHeader(AuthorizeConstant.NICKNAME) String nickname,
                               @RequestParam("userid") int userid) {
        BlogEnum res = friendService.changeFriendByIdAndFriendId(id, userid);
        return res == BlogEnum.DELETE?Result.failR("操作失败"):Result.success(res);
    }
}

